<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="防抖（debounce）：在事件被触发n秒后再执行回调函数，如果在这n秒内又被触发，则重新计时。  应用场景：(1) 用户在输入框中连续输入一串字符后，只会在输入完后去执行最后一次的查询ajax请求，这样可以有效减少请求次数，节约请求资源； (2) window的resize、scroll事件，不断地调整浏览器的窗口大小、或者滚动时会触发对应事件，防抖让其只触发一次；  短信验证码 提交表单 re">
<meta property="og:type" content="article">
<meta property="og:title" content="Hexo">
<meta property="og:url" content="http://example.com/2021/07/18/%E9%98%B2%E6%8A%96%E4%B8%8E%E8%8A%82%E6%B5%81/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="防抖（debounce）：在事件被触发n秒后再执行回调函数，如果在这n秒内又被触发，则重新计时。  应用场景：(1) 用户在输入框中连续输入一串字符后，只会在输入完后去执行最后一次的查询ajax请求，这样可以有效减少请求次数，节约请求资源； (2) window的resize、scroll事件，不断地调整浏览器的窗口大小、或者滚动时会触发对应事件，防抖让其只触发一次；  短信验证码 提交表单 re">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2021-07-18T02:48:04.268Z">
<meta property="article:modified_time" content="2021-07-08T13:54:15.499Z">
<meta property="article:author" content="John Doe">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  
<link rel="stylesheet" href="/css/style.css">

<meta name="generator" content="Hexo 5.4.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://example.com"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-防抖与节流" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2021/07/18/%E9%98%B2%E6%8A%96%E4%B8%8E%E8%8A%82%E6%B5%81/" class="article-date">
  <time datetime="2021-07-18T02:48:04.268Z" itemprop="datePublished">2021-07-18</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
    <div class="article-entry" itemprop="articleBody">
      
        <p><strong>防抖（debounce）</strong>：在事件被触发n秒后再执行回调函数，如果在这n秒内又被触发，则重新计时。</p>
<p> <strong>应用场景</strong>：(1) 用户在输入框中连续输入一串字符后，只会在输入完后去执行最后一次的查询ajax请求，这样可以有效减少请求次数，节约请求资源；</p>
<p>(2) window的resize、scroll事件，不断地调整浏览器的窗口大小、或者滚动时会触发对应事件，防抖让其<strong>只触发一次</strong>；</p>
<ul>
<li>短信验证码</li>
<li>提交表单</li>
<li>resize 事件</li>
<li>input 事件（当然也可以用节流，实现实时关键字查找）</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">// 防抖</span><br><span class="line">var timer = null;</span><br><span class="line">function debounce () &#123;</span><br><span class="line">    clearTimeout(timer);</span><br><span class="line">    timer = setTimeout(function() &#123;</span><br><span class="line">        console.log(&#x27;防抖方法执行了&#x27;)</span><br><span class="line">    &#125;, 200)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>第一次 timer变量保存着这个定时器的标识符（用于关闭当前定时器），如果在200毫秒内调用多次，只会执行最后一次</p>
<p><strong>节流（throttle）</strong>：规定一个单位时间，在这个单位时间内，只能有一次触发事件的回调函数执行，如果在同一个单位时间内某事件被触发多次，只有一次能生效。</p>
<p><strong>应用场景</strong>：(1)鼠标连续不断地触发某事件（如点击），只在单位时间内只触发一次；</p>
<p>(2)在页面的无限加载场景下，需要用户在滚动页面时，每隔一段时间发一次 ajax 请求，而不是在用户停下滚动页面操作时才去请求数据；</p>
<p>(3)监听滚动事件，比如是否滑到底部自动加载更多，用throttle来判断；</p>
<ul>
<li>scroll 事件，单位时间后计算一次滚动位置</li>
<li>input 事件（上面提到过）</li>
<li>播放事件，计算进度条</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">// 节流</span><br><span class="line">var canNext = true;</span><br><span class="line">function throttle () &#123;</span><br><span class="line">    if(!canNext) &#123;</span><br><span class="line">        return;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    canNext = false;</span><br><span class="line">    setTimeout(function () &#123;</span><br><span class="line">        console.log(&#x27;节流方法执行了&#x27;)</span><br><span class="line">        canNext = true;</span><br><span class="line">    &#125;, 200)</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>canNext变量作为状态记录者，当它的值为 false 时，表示上一次调用正在执行，直接跳出本次调用，直到上一次的执行完毕，把true 赋给canNext,这时如果有调用，会执行这次调用。</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2021/07/18/%E9%98%B2%E6%8A%96%E4%B8%8E%E8%8A%82%E6%B5%81/" data-id="ckr8lt4sp000z20qw4kg2ca2e" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2021/07/18/%E5%B7%A5%E7%A8%8B%E7%BB%84%E4%BB%B6%E5%8C%96/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          (no title)
        
      </div>
    </a>
  
  
    <a href="/2021/07/18/%E5%8F%91%E5%B8%83%E8%AE%A2%E9%98%85/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title"></div>
    </a>
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/07/">July 2021</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2021/07/18/%E6%99%BA%E8%83%BD%E5%8C%96%E8%B7%A8%E7%AB%AF/">(no title)</a>
          </li>
        
          <li>
            <a href="/2021/07/18/%E5%9F%9F%E8%A7%A3%E6%9E%90/">(no title)</a>
          </li>
        
          <li>
            <a href="/2021/07/18/%E6%97%A0%E9%99%90%E7%BA%A7%E7%9B%AE%E5%BD%95%E6%A0%91/">(no title)</a>
          </li>
        
          <li>
            <a href="/2021/07/18/%E6%96%87%E5%AD%97%E5%B1%85%E4%B8%AD/">(no title)</a>
          </li>
        
          <li>
            <a href="/2021/07/18/%E5%BE%AE%E6%A0%BC%E5%BC%8F/">(no title)</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2021 John Doe<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  
<link rel="stylesheet" href="/fancybox/jquery.fancybox.css">

  
<script src="/fancybox/jquery.fancybox.pack.js"></script>




<script src="/js/script.js"></script>




  </div>
</body>
</html>